package geographer;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Collections;
import java.util.List;

import javax.swing.JPanel;

/**
 * Project #3
 * CS 2334, 010
 * 03/26/2014
 * <P>
 * This class creates a partition bar to display objects and their data appropriately.
 * </P>
 * @version 1.0
 */
public class PartitionBar extends JPanel
{
	/**
	 * Serial version ID
	 */
	private static final long serialVersionUID = 1L;

	/*The width of the partition bar*/
	int width;
	
	/*The height of the partition bar*/
	int height;
	
	/*The x position of the partition bar*/
	int positionX;
	
	/*The y position of the partition bar*/
	int positionY  = 5;
	
	/*The data that the partition bar will be displaying*/
	List<String> data;
	
	String type;
	
	/**
	 * The constructor for the <code>PartitionBar</code> class.
	 * @param width      The width of the partition bar.
	 * @param height     The height of the partition bar.
	 * @param positionX  The x position of the partition bar.
	 * @param data       The data the partition bar will be displaying.
	 */
	public PartitionBar(int width, int height, int positionX,List<String> data, String type)
	{
		this.width = width;
		this.height = height;
		this.positionX = positionX;
		this.data = data;
		this.type =type;
		Collections.reverse(data);
	}//end constructor
	
	/**
	 * Draws the panel onto the <code>JFrame</code>.
	 * <P>
	 * Algorithm: <br>
	 * 1. Draw the underlying component<br>
	 * 2. Sum the areas to be graphed<br>
	 * 3. Loop through each region, going down the List in order<br>
	 * 4. Calculate positions and sizes<br>
	 * 5. Draw the components<br>
	 * </P>
	 * @param g    The graphics component
	 */
	@Override
	public void paintComponent(Graphics g)
	{
		//Draw the super component
		super.paintComponent(g);

		//The sum of all the areas
		long sum = 0;
		
		//The change in height for every rectangle
		int changeInHeight = 0;
		
		//The first color to be used to fill in the rectangles
		Color color = Color.red;
		
		//float totalPercent = 0;
		
		//Sum all of the areas
		if(type.equals("Area"))
			for(String r : data)
				sum+=Long.parseLong(((GeographicRegion)Database.regions.get(r)).getArea());
		else
			for(String r : data)
				sum+=Long.parseLong(((GeographicRegion)Database.regions.get(r)).getPopulation());
		
		//Draw the components
		for(String reg : data)
		{
			GeographicRegion r = ((GeographicRegion)Database.regions.get(reg));
			Long val;
			if(type.equals("Area"))
				val = Long.parseLong(r.getArea());
			else
				val = Long.parseLong(r.getPopulation());
			
			//Set the pen color
			g.setColor(color);
			
			//Calculate the percentage of the total area of the rectangle for this region
			float percent = (float)val/sum;
			
			//Change to the next y value for the rectangle
			positionY += changeInHeight;
			
			//Calculate the heights for the rectangles and strings to be drawn
			int dividedHeight = (int)(percent*height);
			int stringHeight = (int)(dividedHeight/2) + positionY;
			
			//Draw the components
			g.fillRect(positionX, positionY, width, dividedHeight);
			if(percent>=.02)
			{
				g.drawString(val.toString(), positionX-50, stringHeight);
				g.drawString(r.getName(),positionX+width+25,stringHeight);
			}
			
			//Ready values for the next iteration
			changeInHeight = dividedHeight;
			color = changeColors(color);
		}
		
	}//end paintComponent
	
	/**
	 * Alternate the pen color for the rectangles
	 * <P>
	 * Algorithm: <br>
	 * 1. Check current color<br>
	 * 2. Change the color accordingly<br>
	 * </b>
	 * @param color     The current color
	 * @return          The alternated color value.
	 */
	public Color changeColors(Color color)
	{
		if(color.equals(Color.green))
			color = Color.red;
		else
			color = Color.green;
		return color;
	}//end changeColors
}
